
package edu.whu.powersoap;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

import java.io.File;

public class JobProvider extends ContentProvider {

    private static final String TAG = "JobProvider";

    public static final String AUTHORITY = "powersoap.job";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/");

    public static final String VOLUMN_NUMBER = "number";
    public static final String VOLUMN_STATUS = "status";
    public static final String VOLUMN_CREATE_TIME = "createTime";
    public static final String VOLUMN_FINISH_TIME = "finishTime";
    public static final String VOLUMN_POI_LIST = "poiList";
    public static final String VOLUMN_EXECUTOR = "executor";
    public static final String VOLUMN_PHOTO_PATH = "photoPath";

    private static final String DATABASE_NAME = "job.db";
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE_NAME = "job";

    private DatabaseHelper mOpenHelper;

    @Override
    public boolean onCreate() {
        mOpenHelper = new DatabaseHelper(getContext(), DATABASE_NAME, null,
                DATABASE_VERSION);
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();

        Cursor cursor = db.query(TABLE_NAME, projection, selection,
                selectionArgs, null, null, sortOrder);

        return cursor;
    }

    @Override
    public String getType(Uri uri) {

        Log.i(TAG, "getType---uri=" + uri);
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();

        /* now insert into database */
        long rowId = db.insert(TABLE_NAME, null, values);
        if (rowId > 0) {
            uri = ContentUris.withAppendedId(uri, rowId);
            sendNotify(uri);
            return uri;
        }
        throw new SQLException("Failed to insert row into: " + uri);
    }

    
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count = db.delete(TABLE_NAME, selection, selectionArgs);
        if (count > 0){
            sendNotify(uri);
        }
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count = db.update(TABLE_NAME, values, selection, selectionArgs);

        sendNotify(uri);

        return count;
    }

    private void sendNotify(Uri uri) {
        getContext().getContentResolver().notifyChange(uri, null);

    }

    private static class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE IF NOT EXISTS "
                    + TABLE_NAME
                    + "(number TEXT, status TEXT, createTime LONG, finishTime LONG, poiList TEXT, executor TEXT)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrade database from " + oldVersion + " to " + newVersion);

            // Log.w(TAG, "drop all data!");
            // db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            // onCreate(db);

        }

    }

}
